﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.VCCodeModel;
using System.IO;

namespace GordonKristan.codeclean
{
	public partial class ToolWindowControl : UserControl
	{
		/// <summary>
		/// Default constructor. Initializes then populates treeView.
		/// </summary>
		public ToolWindowControl()
		{
			InitializeComponent();
			//Initialize event handlers
			codeTree.AllowDrop = true;
			codeTree.ItemDrag += new ItemDragEventHandler(codeTree_ItemDrag);
			codeTree.DragEnter += new DragEventHandler(codeTree_DragEnter);
			codeTree.DragOver += new DragEventHandler(codeTree_DragOver);
			codeTree.DragDrop += new DragEventHandler(codeTree_DragDrop);
			codeTree.MouseClick += new MouseEventHandler(codeTree_MouseClick);
			codeTree.GotFocus += new EventHandler(codeTree_GotFocus);
			//Populate the tree
			CodeManagerModule mod = new CodeManagerModule(codeTree);
		}

		/// <summary>
		/// Called to refresh the tree view
		/// </summary>
		private void button1_Click(object sender, EventArgs e)
		{
			CodeManagerModule mod = new CodeManagerModule(codeTree);
		}

		/// <summary>
		/// Called when the control gets focus to refresh it.
		/// </summary>
		private void codeTree_GotFocus(object sender, EventArgs e)
		{
			CodeManagerModule mod = new CodeManagerModule(codeTree);
		}

		/// <summary>
		/// Called when the user clicks the treeView.
		/// </summary>
		protected void codeTree_MouseClick(object sender, MouseEventArgs e)
		{
			Point targetPoint = codeTree.PointToClient(new Point(e.X, e.Y));
			TreeNode node = codeTree.GetNodeAt(targetPoint);
			if (node == null)
			{
				return;
			}
			EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
			if (dte == null || dte.ActiveDocument == null)
			{
				return;
			}
			dte.ExecuteCommand("Edit.Goto", " " + (int)node.Tag);
		}

		/// <summary>
		/// Called at the start of a drag operation.
		/// </summary>
		private void codeTree_ItemDrag(object sender, ItemDragEventArgs e)
		{
			//We drag with the left mouse button
			if (e.Button == MouseButtons.Left)
			{
				codeTree.dragging = true;
				DoDragDrop(e.Item, DragDropEffects.Move);
			}
			 
		}

		/// <summary>
		/// Called when the dragged item enters the destination
		/// treeView. AKA, immediately.
		/// </summary>
		private void codeTree_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = e.AllowedEffect;
		}

		/// <summary>
		/// Called to highlight the node the dragged item is currently on.
		/// </summary>
		private void codeTree_DragOver(object sender, DragEventArgs e)
		{
			// Retrieve the client coordinates of the mouse position.
			Point targetPoint = codeTree.PointToClient(new Point(e.X, e.Y));
			TreeNode node = codeTree.GetNodeAt(targetPoint);
			if (node == null)
			{
				codeTree.y = codeTree.x1 = codeTree.x2 = -1;
			}
			Rectangle box = node.Bounds;
			if (targetPoint.Y >= box.Y + box.Height / 2)
			{
				codeTree.y = box.Y + box.Height;
			}
			else
			{
				codeTree.y = box.Y;
			}
			codeTree.x1 = 10;
			codeTree.x2 = codeTree.Width - 10;
			codeTree.Refresh();
		}

		private void codeTree_DragDrop(object sender, DragEventArgs e)
		{
			codeTree.dragging = false;
			codeTree.Refresh();
		}
	}
}
